/*
    bing.js库
*/
let _b = {
    //将对象转为字符串的方法
    parseObjToStr:function(obj,split='&'){
        return Object.keys(obj).map(function(key){
            return [key,obj[key]].join('=')
        }).join(split)
    },
    //将字符串转为对象的方法
    parseStrToObj:function(str,split='&'){
        let obj = {}
        str.split(split).map(function(item){
            return item.split('=')
        }).forEach(function(item){
            obj[item[0]] = item[1]
        })
        return obj
    },
    //分页方法
    getPaging:function(data,pageIndex,pageSize){
        //算出一共有多少页
        let pageCount = Math.ceil(data.length / pageSize)
        //起始位置
        let start = (pageIndex-1)*pageSize
        //结束位置
        let end = start + pageSize
        let resData =  data.slice(start,end)
        return {
            pageCount:pageCount,
            resData:resData
        }
    },
    //补零方法：如果只有一位数字，在前面补0
    repairZero  : function(val){
        return val < 10 ? "0" + val : val
    },
    //判断是否是闰年方法
    isLeapYear : function(year){
        returnyear % 4 === 0 && year % 100 !== 0 || year % 400 === 0
    },
    //字符串的翻转方法
    strReverse : function(val) {
        return val.split('').reverse().join('')
    },
    //简写日期方法
    simpleDate:function(val,type=0){
        let year = val.getFullYear()
        let month = this.repairZero(val.getMonth()+1)
        let date = this.repairZero(val.getDate())
        let hour = this.repairZero(val.getHours())
        let minute = this.repairZero(val.getMinutes())
        let second = this.repairZero(val.getSeconds())
        let str = [year,month,date].join('-');
        if(type===0){
            str += ' '+[hour,minute,second].join(':')
        }
        return str
    },
    //节流方法
    throttle:function(callback, wait) {
        let date = 0
        return function () {
            if (new Date() - date < wait) {
                return console.log('too busy~');
            }
            callback.apply(this,arguments)
            date = new Date()
        }
    },
    //随机返回颜色的方法
    createColor:function(){
        let str = '0123456789ABCDEF'
        let res = '#'
        for(let i=0;i<6;i++){
            res+= str[Math.floor( Math.random()*16)]
        }
        return res;
    },
    //返回随机数的方法
    getRandom:function(min,max){
        return Math.ceil(Math.random()*(max-min)) + min
    }
}





//给字符串类扩展一个翻转方法
String.prototype.reverse = function(){
    return this.split('').reverse().join('')
}

//给日期类扩展一个返回简写日期的方法
Date.prototype.simpleDate = function(){
    let year = this.getFullYear()
    let month = _b.repairZero(this.getMonth()+1)
    let date = _b.repairZero(this.getDate())
    let hour = _b.repairZero(this.getHours())
    let minute = _b.repairZero(this.getMinutes())
    let second = _b.repairZero(this.getSeconds())
    let str = [year,month,date].join('-')+' '+[hour,minute,second].join(':')
    return str
}

